package ${packageName}.service.impls;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
<#if !isFluxDrive>
import org.apache.dubbo.config.annotation.DubboService;
</#if>
<#if isFluxDrive>
import com.swak.annotation.FluxService;
</#if>
import com.swak.entity.Page;
import com.swak.entity.Parameters;
<#if !isUseMerge>
import com.swak.incrementer.IdGen;
</#if>
import com.swak.service.BaseService;
import com.swak.persistence.QueryCondition;
import ${packageName}.dto.${className}DTO;
import ${packageName}.entity.${className};
import ${packageName}.mapper.${className}Mapper;
import ${packageName}.service.${className}Service;

/**
 * ${className}ServiceImpl.
 * 
 * @author Auto Generator
 * @date ${date}
 */
<#if isFluxDrive>@FluxService<#else>@DubboService</#if>
@Service("${moduleName}${className}Service")
public class ${className}ServiceImpl extends BaseService<${className},${pk}> implements ${className}Service {
	
	@Autowired
	private ${className}Mapper ${functionName}Mapper;
	
	@Override
	public ${className}DTO get(${pk} id){
	   return ${className}DTO.convert(this.${functionName}Mapper.get(id));
	}
	
	<#if !isUseMerge>
	@Override
	@Transactional
	public ${className}DTO save(${className}DTO ${functionName}) {
	    ${className} entity = ${functionName}.transform().setUserId(${functionName}.getPrincipal().getIdAsLong())
				.setUserName(${functionName}.getPrincipal().getName());
		<#if isTenant>
		entity.setTenantId(${functionName}.getPrincipal().getTenantIdAsLong())
				.setTenantName(${functionName}.getPrincipal().getTenantName());
		</#if>
		if (IdGen.isInvalidId(entity.getId())) {
			this.${functionName}Mapper.insert(entity);
		} else {
			this.${functionName}Mapper.update(entity);
		}
		return this.get(entity.getId());
	}
	
	@Override
	@Transactional
	public Integer delete(${className}DTO ${functionName}) {
	    ${className} entity = ${functionName}.transform();
		return this.${functionName}Mapper.delete(entity);
	}
	</#if>
	
	@Override
	public Page<${className}DTO> page(${className}DTO query, Parameters param){
	    QueryCondition qc = new QueryCondition();
	    qc.getCriteria().andEqualTo(${className}.DelFlag, Boolean.FALSE);
	    qc.setOrderByClause(param.orderBy(${className}.DefaultOrderBy));
	    return ${className}DTO.convert(this.${functionName}Mapper.queryForPage(qc, param));
	}
	
	<#if isUseMerge>
	@Override
	@Transactional
	public void merge(${className}DTO merge) {
		this.${functionName}Mapper.merge(merge.transform()); 
	}
	</#if>
}